package models

import (
	"github.com/astaxie/beego/orm"
)
/**
 *  点赞信息结构体
 */
type Thumbup struct {
Id	        int64   `orm:"column(id);pk"`  //设置主键
User_id     int64
Message_id  int64

}

/**
* 公告结构体
*/
//type Notice struct {
//	Id	        int64   `orm:"column(id);pk"`  //设置主键
//	Content     string
//	Time        string
//}

/**
 * 获取点赞信息
 */
func GetThumbList() ([]Thumbup, error) {
	var Thumbup []Thumbup
	_,err := ThumbupObj().All(&Thumbup)
	if err != nil {
		log := UitlLog()
		log.Error("connect db err"+err.Error())
		return nil, err
	}

	return Thumbup, nil
}

/**
 * 点赞
 */
func ThumbU(user_id,message_id int64) (insertId int64, err error) {
	o := orm.NewOrm()
	thumbup := &Thumbup{User_id:user_id,Message_id:message_id }
	//判断点赞是否已经存在
	if o.Read(thumbup,"User_id","Message_id") == nil {
		return 66, nil
	}
	//存入点赞
	id, err := o.Insert(thumbup)
	if err != nil{
		log := UitlLog()
		log.Error("connect db err"+err.Error())
		return 0, err
	}
	//return user
	//存入成功
	id = 6
	return id, nil
}
/**
 * 根据ID取消点赞
 */
func DelThumb(user_id,message_id int64) (int64, error) {
	num,err := ThumbupObj().Filter("User_id",user_id).Filter("Message_id",message_id).Delete()
	if err != nil {
		log := UitlLog()
		log.Error("connect db err"+err.Error())
		return 0, err
	}
	return num, nil
}
/**
 *  MessagePad 表实例化
 */
func ThumbupObj() orm.QuerySeter {
	return orm.NewOrm().QueryTable(new(Thumbup))
}
